#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/11/7 15:32
@desc
"""

from setting import hive_db
from postgreopr import getconninfo
from postgreopr import PostgreOpr
from errors import TypeMappingError


def genHiveCols(cols):
    columns_str_arr = []
    for col_name, data_type, character_maximum_length, numeric_precision, numeric_scale in cols:
        if data_type == 'character varying':
            columns_str_arr.append(col_name + " VARCHAR" + (
                "(" + str(character_maximum_length) + ")" if character_maximum_length is not None else ""))
        elif data_type == 'bytea':
            columns_str_arr.append(col_name + " BINARY")
        elif data_type == 'double precision':
            columns_str_arr.append(col_name + " DOUBLE")
        elif data_type == 'character':
            columns_str_arr.append(col_name + " String")
        elif data_type == 'boolean':
            columns_str_arr.append(col_name + " BOOLEAN")
        elif data_type == 'timestamp':
            columns_str_arr.append(col_name + " Timestamps")
        elif data_type == 'date':
            columns_str_arr.append(col_name + " Dates")
        elif data_type == 'real':
            columns_str_arr.append(col_name + " FLOAT")
        elif data_type == 'smallint':
            columns_str_arr.append(col_name + " SMALLINT")
        elif data_type == 'integer':
            columns_str_arr += col_name + " INTEGER"
        elif data_type == 'bigint':
            columns_str_arr += col_name + " BIGINT"
        elif data_type == 'numeric':
            columns_str_arr += col_name + " DECIMAL" + (
                "(" + str(numeric_precision) if numeric_precision is not None else "") + genDecmalScope(numeric_precision, numeric_scale)
        else:
            raise TypeMappingError('类型映射错误，你的原始表的字段类型必须为:character varying,bytea,boolean,timestamp,date,real,smallint,integer,bigint,numeric')
    return columns_str_arr


def genDecmalScope(numeric_precision, numeric_scale):
    if numeric_scale is not None:
        return str(numeric_scale) + ")"
    elif numeric_precision is not None:
        return ")"
    else:
        return ""


def genHiveCrt(databasename, tablename, columns_str_arr, commentStr):
    columnsStr = '\n\t,'.join(columns_str_arr)
    crt = "CREATE TABLE " + databasename + "." + tablename + "(\n\tDEAL_DATE STRING COMMENT '处理日期'\n\t," + columnsStr + ")\n" + " COMMENT '" + commentStr + "'\nPARTITIONED BY(DEAL_DATE_P DATE COMMENT '处理日期')" + "\nROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'" + "\nSTORED AS PARQUET\n" + """TBLPROPERTIES("PARQUET.COMPRESSION" = "SNAPPY");"""
    return crt


def genHiveTmpCrt(databasename, tablename, columns_str_arr):
    columnsStr = '\n\t,'.join(columns_str_arr)
    crt = "CREATE TABLE " + databasename + "." + tablename + "(" + columnsStr + ")\n" + " ROW  FORMAT DELIMITED FIELDS TERMINATED BY '\\t' STORED AS TEXTFILE;"
    return crt


def gen_cycle_del_sql(databasename, tablename, cyclevalue):
    return "ALTER TABLE %s.%s DROP IF EXISTS PARTITION (DEAL_DATE_P='%s');" % (databasename, tablename, cyclevalue)


def gen_cycle_add_sql(databasename, tablename, cyclevalue):
    return "ALTER TABLE %s.%s ADD IF NOT EXISTS PARTITION (DEAL_DATE_P='%s');" % (databasename, tablename, cyclevalue)


dbinfo = getconninfo(hive_db)
pdb = PostgreOpr(dbinfo)
tasks = pdb.getSynTask()

for job_id, job_type, job_detail, user_name, table_name, status, rate, resultdb in tasks:
    print("获取作业信息")
    print(job_id, job_type, job_detail, user_name, table_name, status, rate, resultdb)
    resdbinfo = getconninfo(resultdb)
    rdb = PostgreOpr(resdbinfo)
    ddl = rdb.getddl('public', table_name)
    print("获取表元数据")
    print(ddl)
    try:
        # 获得原始表字段信息
        colsinfo = genHiveCols(ddl)
    except TypeMappingError as e:
        print(e)

    # 获得hive临时表建表语句
    crttmpddl = genHiveTmpCrt('ptemp3', table_name, colsinfo)
    print("获得hive临时表建表语句")
    print(crttmpddl)
    print("删除hive临时表")
    print("创建hive临时表")
    print("调度DATAX同步")
    print("判断正式表是否存在")
    # 获得hive正式表建表语句
    crtddl = genHiveCrt('spdiertargetdb', 'tb_' + table_name + "_parquet", colsinfo, table_name)
    print("获得hive正式表建表语句")
    print(crtddl)
    print("清除hive表历史分区")
    print("清除hive表当前分区")
    print("创建hive表当前分区")
    print("清除hive表当前分区")
    print("正式hive表插入临时表数据")
    print("删除临时表")
    print("更新hivejob表")
    del rdb
    print("==============================")



